cmake_minimum_required(VERSION 3.10) # 限制cmake最低版本

project(Timer
    LANGUAGES C CXX
    VERSION 0.1
) # 项目名称

add_definitions(-DVERSION="${CMAKE_PROJECT_VERSION}")

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True) # 强制使用C++17

# 设置编译器选项
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
    add_compile_options(
        "-Wall"      # 开启所有警告信息
        "-Wextra"
        "-Wpedantic" # 检查ISO C和ISO C++标准的严格遵守。
        "-std=c++17" # c++17标准
    )
    message("编译器：g++")
else()
    message("编译器：" ${CMAKE_CXX_COMPILER})
endif()
message("编译器版本：" ${CMAKE_CXX_COMPILER_VERSION})

# 设置生成路径
set(RELEASE_DIR ${CMAKE_SOURCE_DIR}/release)

# 导入Qt6
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)

# 导入Qt6的模块
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
# set(CMAKE_AUTOUIC ON)

# 项目相关库
## time 库, 用于时间处理
add_subdirectory(./time)
## gui  库，用于GUI界面
add_subdirectory(./gui)

# 项目头文件
include_directories(${CMAKE_SOURCE_DIR}/time)
include_directories(${CMAKE_SOURCE_DIR}/gui)
include_directories(${CMAKE_SOURCE_DIR}/core)

# 设置Linux平台下的动态连接路径

# 项目可执行文件
add_executable(Timer main.cpp
    core/common.cpp
    core/config.cpp
)

# 为Linux平台设置Rpath路径
if(LINUX)
    set_target_properties(Timer PROPERTIES
        # BUILD_RPATH "$ORIGIN/../lib/${PROJECT_NAME}"
        INSTALL_RPATH "$ORIGIN/../lib/${PROJECT_NAME}"
    )
endif()

# 可执行文件输出路径
set(EXECUTABLE_OUTPUT_PATH ${RELEASE_DIR}/bin)

# 链接Qt6库 和 项目相关库
target_link_libraries(Timer Qt6::Widgets gui)

# 安装
if(LINUX)
    # include(GNUInstallDirs)
    message("编译平台：Linux")
    message("可执行文件安装目录 : " ${CMAKE_INSTALL_PREFIX}/bin)
    message("动态库安装目录 : " ${CMAKE_INSTALL_PREFIX}/lib/${PROJECT_NAME})
    # 目录结构：
    # Timer/
    #├── bin
    #│   └── Timer
    #└── lib
    #    └── Timer
    #        ├── libgui.so
    #        └── libtime.so
    # 安装动态库
    install(TARGETS gui LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/${PROJECT_NAME})
    install(TARGETS time LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/${PROJECT_NAME})
    # 安装可执行文件
    install(TARGETS Timer RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
elseif(WIN32)
    # 安装动态库
    install(TARGETS gui LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
    install(TARGETS time LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/bin})
    install(TARGETS Timer RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
endif()

# 测试
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    message("构建测试用例！")
    add_subdirectory(test/)
endif()
